package com.yoomiito.app.widget.videoplayer

import android.content.Context
import android.util.AttributeSet
import android.view.View

import cn.jzvd.JzvdStd
import com.yoomiito.app.R
import com.yoomiito.app.utils.L
import java.security.cert.X509Certificate
import javax.net.ssl.*


class VideoPlayer(context: Context, attrs: AttributeSet?=null) : JzvdStd(context, attrs) {
    companion object{
        const val START_PLAY = 1
        const val PLAY_COMPLETE = 2
        const val PLAYING = 3
        const val UnPlay = 4
        const val PLAY_PROGRESS = 10
    }
    var playStateListener:(Int, Int)->Unit={ _, _->}
    var isAutoPlay = false

    override fun init(context: Context?) {
        super.init(context)
        replayTextView.text = "重播"
        mRetryBtn.text = "点击重试"
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.start-> playStateListener(START_PLAY,0)
            cn.jzvd.R.id.fullscreen->L.i("开始全屏播放")
        }
    }

    override fun onStatePlaying() {
        super.onStatePlaying()
        playStateListener(PLAYING,0)
    }

    override fun onStateAutoComplete() {
        super.onStateAutoComplete()
        playStateListener(PLAY_COMPLETE,0)
    }

    override fun onAutoCompletion() {
        super.onAutoCompletion()
        if (isAutoPlay) startVideo()
    }

    //播放进度
    override fun onProgress(progress: Int, position: Long, duration: Long) {
        super.onProgress(progress, position, duration)
//        L.i("进度:$progress   PO:$position   Duration:$duration")
//        L.i("时间:${getDuration()}   当前时间：${currentPositionWhenPlaying}")
        playStateListener(PLAY_PROGRESS,(currentPositionWhenPlaying/1000).toInt())
    }
}
